package com.shuo.dao.impl;

import com.shuo.dao.UserDao;
import com.shuo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by linshuo on 2018/3/27.
 */
@Repository
public class UserDaoImpl implements UserDao {

    private static final String tableName = "user";

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public long add(User user) {
        String sql = "INSERT INTO user(name, age) values(?, ?)";
        return jdbcTemplate.update(sql,
                user.getName(), user.getAge());

    }

    @Override
    public long delete(Long id) {
        return this.deleteBy("id", id);
    }

    @Override
    public long deleteBy(String field, Object value) {
        String sql = "DELETE FROM " + tableName + " WHERE " + field + "=?";
        return jdbcTemplate.update(sql, value);
    }

    @Override
    public long update(User user) {
        return jdbcTemplate.update("UPDATE " + tableName + " SET name=? ,age=? WHERE id=?",
                user.getName(), user.getAge(), user.getId());
    }

    @Override
    public Long count() {
        String sql = "SELECT count(*) FROM " + tableName;
        return jdbcTemplate.queryForObject(sql, Long.class);
    }

    @Override
    public Long countBy(String field, Object value) {
        Object[] args = {value};
        String sql = "SELECT count(*) FROM " + tableName + " WHERE " + field + " = ? ";
        return jdbcTemplate.queryForObject(sql, args, Long.class);
    }

    @Override
    public User get(Long id) {
        return this.getBy("id", id);
    }

    @Override
    public User getBy(String field, Object value) {
        List<User> userList = this.listBy(field, value);
        if (userList != null) {
            return userList.get(0);
        } else {
            return null;
        }
    }

    @Override
    public User getByAnd(String field1, Object value1, String field2, Object value2) {
        List<User> userList = this.listByAnd(field1, value1, field2, value2);
        if (userList != null) {
            return userList.get(0);
        } else {
            return null;
        }
    }

    @Override
    public User getByOr(String field1, Object value1, String field2, Object value2) {
        List<User> userList = this.listByOr(field1, value1, field2, value2);
        if (userList != null) {
            return userList.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<User> listAll() {
        String sql = "SELECT * FROM " + tableName;
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper(User.class));
        if (list != null && list.size() > 0) {
            return list;
        } else {
            return null;
        }
    }

    @Override
    public List<User> listBy(String field, Object value) {
        String sql = "SELECT * FROM " + tableName + " WHERE " + field + " = ?";
        List<User> list = jdbcTemplate.query(sql, new Object[]{value}, new BeanPropertyRowMapper(User.class));
        if (list != null && list.size() > 0) {
            return list;
        } else {
            return null;
        }
    }

    @Override
    public List<User> listByAnd(String field1, Object value1, String field2, Object value2) {
        String sql = "SELECT * FROM " + tableName + " WHERE " + field1 + " = ? AND WHERE " + field2 + "= ?";
        List<User> list = jdbcTemplate.query(sql, new Object[]{value1, value2}, new BeanPropertyRowMapper(User.class));
        if (list != null && list.size() > 0) {
            return list;
        } else {
            return null;
        }
    }

    @Override
    public List<User> listByOr(String field1, Object value1, String field2, Object value2) {
        String sql = "SELECT * FROM " + tableName + " WHERE " + field1 + " = ? OR WHERE " + field2 + "= ?";
        List<User> list = jdbcTemplate.query(sql, new Object[]{value1, value2}, new BeanPropertyRowMapper(User.class));
        if (list != null && list.size() > 0) {
            return list;
        } else {
            return null;
        }
    }


}